/*
 * TccUserAS.java
 *
 * Created on 08/09/2007, 19:00:36
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tcc.applicationService.tccsystemuser;

import java.util.List;
import javax.persistence.EntityTransaction;
import tcc.applicationService.AbstractAS;
import tcc.dao.login.TCCSystemUserDAO;
import tcc.domain.login.TCCSystemUser;
import tcc.exception.system.TccASException;
import tcc.exception.system.TccSystemException;
import tcc.util.PersistenceUtil;


 public class TccSystemUserAS extends AbstractAS {

    private static TccSystemUserAS instance;

    public static synchronized TccSystemUserAS getInstance() {
        if (instance == null) {
            instance = new TccSystemUserAS();
        }
        return instance;
    }

    public void salvarFuncionario(TCCSystemUser funcionario) throws TccSystemException {
        EntityTransaction tx = PersistenceUtil.getInstance().getTransaction();
        try {
            tx.begin();
            new TCCSystemUserDAO().save(funcionario);
            tx.commit();
        } catch (Exception ex) {
            tx.rollback();
            throw new TccASException(ex);
        }
    }

    public void removerFuncionanrio(TCCSystemUser funcionario) throws TccASException {
            EntityTransaction tx = PersistenceUtil.getInstance().getTransaction();
        try {
            tx.begin();
            new TCCSystemUserDAO().delete(funcionario);
            tx.commit();
        } catch (Exception ex) {
            tx.rollback();
            throw new TccASException(ex);
        }
    }

    public List<TCCSystemUser> listarFuncionarios() throws TccASException {
        try {
            return new TCCSystemUserDAO().findAll();
        } catch (Exception ex) {
            throw new TccASException(ex);
        }
    }

    public TCCSystemUser buscarPorId(Long Id) throws TccASException {
        try {
            return new TCCSystemUserDAO().findById(Id);
        } catch (Exception ex) {
            throw new TccASException(ex);
        }
    }
}